/*
 * @Author: DESKTOP-05FP536\Administrator 18368095041@163.com
 * @Date: 2025-01-12 20:38:18
 * @LastEditors: DESKTOP-05FP536\Administrator 18368095041@163.com
 * @LastEditTime: 2025-01-13 09:54:29
 * @FilePath: \road-admin-web\src\views\common\iframe\helpers\v6.0.6\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import router from '@/router/index';

export interface Point {
    lng: number;
    lat: number;
}

export type PointWithUid = Point & {
    parentPolylineUid?: string;
    parentPolygonUid?: string;
} & { uid: string };

export type PolylineWithUid = {
    polyline: PointWithUid[];
    uid: string;
};

export type PolygonWithUid = {
    polygon: PointWithUid[];
    uid: string;
};

export interface Layer {
    name: string;
    points: PointWithUid[];
    polylines: PolylineWithUid[];
    polygons: PolygonWithUid[];
}

interface SelectMapPointData {
    type: '/map/drawView?mode=singlePoint' | '/map/drawView?mode=multiPoint';
    data: Layer;
}

export const selectMapPoint = (mode: 'single' | 'multi', value?: string) => {
    let resolveFn: (data: SelectMapPointData | undefined) => void;
    const p = new Promise<SelectMapPointData | undefined>((resolve) => {
        resolveFn = resolve;
    });
    const targetRoute = router.resolve({
        path: '/iframe',
        query: {
            url: encodeURI(
                import.meta.env.VITE_UTILS_WEB +
                    `/map/drawView?mode=${mode}Point&value=${value}`
            )
        }
    });
    let referenceWindow = window.open(targetRoute.href);
    const handlePoint = (e: any) => {
        console.log(e);
        if (
            [
                '/map/drawView?mode=singlePoint',
                '/map/drawView?mode=multiPoint'
            ].includes(e.data.type)
        ) {
            resolveFn(e.data);
            console.log('选到了');
            referenceWindow?.close();
        }
        if (e.data.type === 'close') {
            console.log('关闭了');
            resolveFn(undefined);
            referenceWindow?.removeEventListener('message', handlePoint);
            referenceWindow = null;
        }
    };
    referenceWindow?.addEventListener('message', handlePoint);
    return p;
};

interface SelectMapPolylineData {
    type:
        | '/map/drawView?mode=singlePolyline'
        | '/map/drawView?mode=multiPolyline';
    data: Layer;
}

export const selectMapPolyline = (mode: 'single' | 'multi', value?: string) => {
    let resolveFn: (data: SelectMapPolylineData | undefined) => void;
    const p = new Promise<SelectMapPolylineData | undefined>((resolve) => {
        resolveFn = resolve;
    });
    const targetRoute = router.resolve({
        path: '/iframe',
        query: {
            url: encodeURI(
                import.meta.env.VITE_UTILS_WEB +
                    `/map/drawView?mode=${mode}Polyline&value=${value}`
            )
        }
    });
    let referenceWindow = window.open(targetRoute.href);
    const handlePolyline = (e: any) => {
        console.log(e);
        if (
            [
                '/map/drawView?mode=singlePolyline',
                '/map/drawView?mode=multiPolyline'
            ].includes(e.data.type)
        ) {
            resolveFn(e.data);
            console.log('选到了');
            referenceWindow?.close();
        }
        if (e.data.type === 'close') {
            console.log('关闭了');
            resolveFn(undefined);
            referenceWindow?.removeEventListener('message', handlePolyline);
            referenceWindow = null;
        }
    };
    referenceWindow?.addEventListener('message', handlePolyline);
    return p;
};

interface SelectMapPolygonData {
    type:
        | '/map/drawView?mode=singlePolygon'
        | '/map/drawView?mode=multiPolygon';
    data: Layer;
}

export const selectMapPolygon = (mode: 'single' | 'multi', value?: string) => {
    let resolveFn: (data: SelectMapPolygonData | undefined) => void;
    const p = new Promise<SelectMapPolygonData | undefined>((resolve) => {
        resolveFn = resolve;
    });
    const targetRoute = router.resolve({
        path: '/iframe',
        query: {
            url: encodeURI(
                import.meta.env.VITE_UTILS_WEB +
                    `/map/drawView?mode=${mode}Polygon&value=${value}`
            )
        }
    });
    let referenceWindow = window.open(targetRoute.href);
    const handlePolygon = (e: any) => {
        console.log(e);
        if (
            [
                '/map/drawView?mode=singlePolygon',
                '/map/drawView?mode=multiPolygon'
            ].includes(e.data.type)
        ) {
            resolveFn(e.data);
            console.log('选到了');
            referenceWindow?.close();
        }
        if (e.data.type === 'close') {
            console.log('关闭了');
            resolveFn(undefined);
            referenceWindow?.removeEventListener('message', handlePolygon);
            referenceWindow = null;
        }
    };
    referenceWindow?.addEventListener('message', handlePolygon);
    return p;
};
